const db = wx.cloud.database();
const app = getApp();
let chartLine;
let chartLine1;

function getOption(xData, data_cur, tmax, tmin) {
  var option = {
    backgroundColor: "#fff",
    color: ["#37A2DA", "#f2960d", "#67E0E3", "#9FE6B8"],

    grid: {
      top: '15%',
      left: '1%',
      right: '3%',
      bottom: '60rpx',
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: xData || [],

    },
    yAxis: {
      x: 'center',
      name: '人',
      type: 'value',
      min: tmin,
      max: tmax
    },
    series: [{
      name: '今日',
      zIndex: 2,
      type: 'line',
      smooth: true,
      symbolSize: 0,
      data: data_cur || []
    }]
  };
  return option;
}

function getOption1(xData, data_cur, tmax, tmin) {
  var option = {
    backgroundColor: "#fff",
    color: ["#37A2DA", "#f2960d", "#67E0E3", "#9FE6B8"],

    grid: {
      top: '15%',
      left: '1%',
      right: '2%',
      bottom: '60rpx',
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: xData || [],
    },
    yAxis: {
      x: 'center',
      name: '人',
      type: 'value',
      min: tmin,
      max: tmax
    },
    series: [{
      name: '今日',
      zIndex: 2,
      type: 'bar',
      smooth: true,
      symbolSize: 0,
      barWidth: 30,
      data: data_cur || []
    }]
  };
  return option;
}

Page({
  data: {
    ecLine1: {
      onInit: function (canvas, width, height) {
        //初始化echarts元素，绑定到全局变量，方便更改数据
        chartLine1 = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chartLine1);
        var q = [];
        var w = [];
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/chaxun/getlist?actid=40747&callback=?', //接收url
          data: {
            phone: '15205451183',
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson.lists)

            var i = 0
            for (var i = 0; i < theJson.lists.length; i++) {
              q.push(theJson.lists[i].q)
            }

            console.log(theJson.lists[0])
            
            w.push(theJson.lists[0].w)
            for (var a = 1; a < theJson.lists.length; a++) {
              var b = theJson.lists[a].w - theJson.lists[a - 1].w
              w.push(b)
            }

            console.log(w)

            var xData = q;
            var data_cur = w;
            var tmin = theJson.lists[1].min;
            var tmax = theJson.lists[1].max;
            console.log(tmin)
            console.log(tmax)
            // 方法一：
            var option = getOption1(xData, data_cur, tmax, tmin);
            chartLine1.setOption(option);
          }
        })



      }
    },
    ecLine: {
      onInit: function (canvas, width, height) {
        //初始化echarts元素，绑定到全局变量，方便更改数据
        chartLine = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chartLine);
        var q = [];
        var w = [];
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/chaxun/getlist?actid=40747&callback=?', //接收url
          data: {
            phone: '15205451183',
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson.lists)

            var i = 0
            for (var i = 0; i < theJson.lists.length; i++) {
              q.push(theJson.lists[i].q)
              w.push(theJson.lists[i].w)
            }
            var xData = q;
            var data_cur = w;
            var tmin = theJson.lists[0].min;
            var tmax = theJson.lists[0].max;
            // 方法一：
            var option = getOption(xData, data_cur, tmax, tmin);
            chartLine.setOption(option);
          }
        })



      }
    },
    area: '',
    geneal: '',
  },


  tozhiwei() {
    var that = this
    wx.navigateTo({
      url: '../zhiwei/sy/sy?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page,
    })
  },
  toggyy() {
    var that = this
    wx.navigateTo({
      url: '../liliaoweisheng/yuyue/yuyue?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page,
    })
  },
  tokq() {
    var that = this
    wx.navigateTo({
      url: '../liliaoweisheng/kqsy/kqsy?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page,
    })
  },

  tozykm() {
    var that = this
    wx.navigateTo({
      url: '../liliaoweisheng/zykm/zykm?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page,
    })
  },
  tojmfsx() {
    var that = this
    wx.navigateTo({
      url: '../index/index?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page,
    })
  },
  to191() {
    wx.navigateToMiniProgram({
      appId: 'wxca86930ec3e80717', //19
      path: 'page/course/details?id=281174',
      envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
      success(res) {
        console.log('打开成功！')
      }
    })
  },
  to192() {
    wx.navigateToMiniProgram({
      appId: 'wxca86930ec3e80717', //19
      path: 'page/course/details?id=280244',
      envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
      success(res) {
        console.log('打开成功！')
      }
    })
  },
  toweb1() {
    wx.navigateTo({
      url: '../web1/web1',
    })
  },
  toyike() {
    wx.navigateTo({
      url: '../lianjie/web1/web1',
    })
  },
  toyilu() {
    wx.navigateTo({
      url: '../lianjie/web2/web2',
    })
  },
  toyiziliaodai() {
    wx.navigateTo({
      url: '../lianjie/web3/web3',
    })
  },
  tojiaoliuqun() {
    wx.navigateTo({
      url: '../lianjie/web4/web4',
    })
  },
  tofldy() {
    var that = this
    wx.navigateTo({
      url: '../liliaoweisheng/fulidaiyu/fulidaiyu?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page,
    })
  },
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }

    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlist?actid=40731&callback=?', //接收url
      data: {
        phone: '15205451183',
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        // console.log(theJson.lists)
        let list = theJson.lists
        that.setData({
          mingcheng1: list[0].mingcheng,
          shuju1: list[0].shuju,
          xinxi1: list[0].xinxi,
          mingcheng2: list[1].mingcheng,
          shuju2: list[1].shuju,
          xinxi2: list[1].xinxi,
          mingcheng3: list[2].mingcheng,
          shuju3: list[2].shuju,
          xinxi3: list[2].xinxi,
        })
      }
    })
  },
})